using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.ComponentModel;
using System.IO;

using EnterpriseDT.Net.Ftp;

namespace FreeboxHDVideoPlayer
{
    public class FreeBoxHDFTP : IDisposable
    {
        private string host;
        private int port;
        private string userName;
        private string userPassword;
        private string videoDir = "Disque dur/Enregistrements/";
        private FTPClient ftp = null;

        public FTPClient FTPClient
        {
            get
            {
                return this.ftp;
            }
        }

        public string Host
        {
            get
            {
                return this.host;
            }
        }

        public string UserName
        {
            get
            {
                return this.userName;
            }
        }

        public string VideoDir
        {
            get
            {
                return "/" + videoDir;
            }
        }

        public string VideoDirFullPath
        {
            get
            {
                return "ftp://" + UserName + "@" + Host + VideoDir;
            }
        }

        public FreeBoxHDFTP(String host, int port, string user, string password)
        {
            this.host = host;
            this.port = port;
            this.userName = user;
            this.userPassword = password;
            if (!string.IsNullOrEmpty(this.host) && this.host.StartsWith("ftp://"))
            {
                this.host = this.host.Replace("ftp://", "");
            }
        }

        public void Connect()
        {
            ftp = new FTPClient(host, port);
        }

        public void Login()
        {
            ftp.Login(userName, userPassword);
            // set up passive BINARY transfers
            ftp.ConnectMode = FTPConnectMode.PASV;
            ftp.TransferType = FTPTransferType.BINARY;
        }

        public string Pwd()
        {
            return ftp.Pwd();
        }

        public void GotoVideoHome()
        {
            this.ChDir(this.videoDir);
        }

        public bool CheckConnected()
        {
            bool reconnect = false;
            if (!ftp.IsConnected || !ftp.Connected)
            {
                ftp.Connect();
                reconnect = true;
            }
            return reconnect;
        }

        public void ChDir(string path)
        {
            ftp.ChDir(path);
        }

        public void ChDirWithMkdir(string path)
        {
            try
            {
                ftp.ChDir(path);
            }
            catch (FTPException ftpEx)
            {
                if (ftpEx.ReplyCode == 550)
                {
                    ftp.MkDir(path);
                    ftp.ChDir(path);
                }
                else
                {
                    throw ftpEx;
                }
            }
        }

        public List<String> Dir()
        {
            string[] files = ftp.Dir();
            List<String> filesList = new List<String>(files.Length);
            foreach (string file in files)
            {
                if (!".".Equals(file) && !"..".Equals(file))
                {
                    filesList.Add(file);
                }
            }
            return filesList;
        }

        public void CdUp()
        {
            ftp.CdUp();
        }

        public void Put(Stream stream, string name)
        {
            using (stream)
            {
                ftp.Put(stream, name);
                stream.Close();
            }
        }

        public void Delete(string name)
        {
            ftp.Delete(name);
        }

        public void Quit()
        {
            if (ftp != null)
            {
                ftp.QuitImmediately();
            }
        }

        public void Dispose()
        {
            this.Quit();
        }
    }
}
